﻿using System;
using System.Text;
using System.Data;
using System.IO;
using System.Drawing;
using System.Drawing.Text;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;
using iTextSharp.text.pdf;
using Ysb.Components;
//using Aspose.Pdf;
//using Aspose.Pdf.Kit;
//using System.Windows.Forms;
//using System.Threading;
public partial class Admin_ToPdf : System.Web.UI.Page
{
    Code39 cd = new Code39();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ToPdf();
            // CreetImage();


        }
    }
    private System.Drawing.Bitmap bitmap;
    private string url;
    private int w = 1024, h = 1800;
    //public void setBitmap()
    //{


    //    using (WebBrowser wb = new WebBrowser())
    //    {
    //        wb.Width = w;
    //        wb.Height = h;
    //        wb.ScrollBarsEnabled = false;
    //        wb.Navigate(url);
    //        //确保页面被解析完全
    //        while (wb.ReadyState != WebBrowserReadyState.Complete)
    //        {
    //            System.Windows.Forms.Application.DoEvents();
    //        }
    //        bitmap = new System.Drawing.Bitmap(w, h);
    //        wb.DrawToBitmap(bitmap, new System.Drawing.Rectangle(0, 0, w, h));
    //        wb.Dispose();
    //    }
    //}
    void ToPdf()
    {
        //如果是分单 需要传参数 sep=true;

        string no = Request.QueryString["no"].ToString();
        List<TnoInfo> th = new List<TnoInfo>();
        if (Request.QueryString["sep"] == null)
        {
            //默认批量哪类
            th = new WayBillService().GetList(no, 4);
        }
        else
        {

            //其他类
            string[] Nnum = no.Split(',');


            for (int i = 0; i < Nnum.Length; i++)
            {
                if (Nnum[i].Trim() != "")
                {
                    TnoInfo ti = new TnoInfo();
                    ti.Name = Nnum[i].Trim();
                    ti.No = Nnum[i].Trim();
                    ti.Num = 1;

                    if (Request.QueryString["sep"] != null && Request.QueryString["sep"].ToString() == "true")
                    {

                        ti.Sep = true;
                    }
                    else
                    {
                        ti.Sep = false;
                    }
                    ti.Tno = Nnum[i].Trim();
                    th.Add(ti);
                }

            }


        }



        iTextSharp.text.Document doc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 0, 0, 0, 0);//左右上下
        //iTextSharp.text.Document doc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 9, 18, 36, 36);//左右上下
        MemoryStream ms = new MemoryStream();
        PdfWriter writer = PdfWriter.GetInstance(doc, ms);
        writer.CloseStream = false;
        doc.Open();
        try
        {


            for (int i = 0; i < th.Count; i++)
            {
                DAL.SysTemError.SystemLog(i.ToString());

                DataTable dt = null;
                if (th[i].Sep)
                {
                    string sql = " ''as DimensionsWeight, AddressType, CreateDate,  SepNo as TrackingNo, ACNo  as AccountNo ,SendBy  as  Sender,USATelNo as FromPhoneNo,FlightNo ,USACompany as FromCompany,USAAddress as FromAddress,  Logo,    CHNReceiver as  Receiver,CHNTelNo as  ToPhoneNo, CHNCompany as  ToCompany,CHNAddress as  ToAddress,Packing,TotalMoney as DeclarePrice,Origin,Dest as Destination, Pieces as NoOfPiece,Weight as  TotalWeight,MeasurementL as DimensionsL,MeasurementW as DimensionsW,MeasurementH as DimensionsH,ChargedWt,PayType as Payment";
                    //分单
                    dt = new PublicSqlService().GetData(sql, "Tbl_SepBill", "SepNo='" + th[i].No.Trim() + "'");
                }
                else
                {

                    dt = new PublicSqlService().GetData("*", "Tbl_WayBill", "TrackingNo='" + th[i].No.Trim() + "'");
                }
                if (dt.Rows.Count != 1)
                {
                    continue;
                }
                string path = ApplicationInfo.PhysicalApplicationPath + "\\TempLet\\shipping20101205.jpg";
                //string path = ApplicationInfo.PhysicalApplicationPath + "\\TempLet\\trans-pic.jpg";
                System.Drawing.Image image = System.Drawing.Image.FromFile(path);

                ToImage(image, dt, th[i].Sep);

                iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(image, System.Drawing.Imaging.ImageFormat.Jpeg);
                //   iTextSharp.text.Image img2 = iTextSharp.text.Image.GetInstance(GetBkImage(), System.Drawing.Imaging.ImageFormat.Jpeg);

                img.ScalePercent(50);//560 630
                doc.Add(img);

                image.Dispose();


            }
        }
        catch (Exception err)
        {
            DAL.SysTemError.SystemLog(err.Message);
            throw new Exception(err.Message);
        }
        finally
        {
            doc.Close();
            using (FileStream fs = new FileStream(Server.MapPath("out.pdf"), FileMode.Create))
            {
                ms.Position = 0;
                byte[] bit = new byte[ms.Length];
                ms.Read(bit, 0, (int)ms.Length);
                fs.Write(bit, 0, bit.Length);
            }
            ViewPdf(ms);
        }




    }

    void ToImage(System.Drawing.Image image, DataTable dt, bool sep)
    {
        #region 老格式 2010-12-27注释
        /*
         
        int y = 824;


        Graphics g = Graphics.FromImage(image);

        Font f = new Font("宋体", 18, FontStyle.Bold);
        Font bgf = new Font("宋体", 23, FontStyle.Bold);
        Brush b = new SolidBrush(Color.Black);
        g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;


        if (sep)
        {
            g.DrawImage(GetImage(dt.Rows[0]["Logo"].ToString()), 25, 21, 319, 82);
            g.DrawImage(GetImage(dt.Rows[0]["Logo"].ToString()), 25, 21 + y, 319, 82);
        }
        else
        {
            g.DrawImage(GetImage("uxpdflogo.jpg"), 25, 21, 319, 82);
            g.DrawImage(GetImage("uxpdflogo.jpg"), 25, 21 + y, 319, 82);

        }

        g.DrawImage(cd.CreateBarCode(dt.Rows[0]["TrackingNo"].ToString(), "", 0, 0), 470, 40);//条形码
        g.DrawImage(cd.CreateBarCode(dt.Rows[0]["TrackingNo"].ToString(), "", 0, 0), 470, 40 + y);//条形码

        g.DrawString(dt.Rows[0]["TrackingNo"].ToString(), bgf, b, 555, 115);//单号
        g.DrawString(dt.Rows[0]["TrackingNo"].ToString(), bgf, b, 555, 115 + y);//单号

        g.DrawString(new Tools().StringToTime(dt.Rows[0]["CreateDate"].ToString()).ToShortDateString(), f, b, 10, 158);//寄件日期
        g.DrawString(new Tools().StringToTime(dt.Rows[0]["CreateDate"].ToString()).ToShortDateString(), f, b, 10, 158 + y);//寄件日期

        if (sep)
        {
            if (dt.Rows[0]["AddressType"].ToString().Trim() != "1")
            {
                //非杭州地区的 需要显示账号
                g.DrawString(dt.Rows[0]["AccountNo"].ToString(), f, b, 204, 158);//寄件账号
                g.DrawString(dt.Rows[0]["AccountNo"].ToString(), f, b, 204, 158 + y);//寄件账号
            }

        }
        else
        {
            g.DrawString(dt.Rows[0]["AccountNo"].ToString(), f, b, 204, 158);//寄件账号
            g.DrawString(dt.Rows[0]["AccountNo"].ToString(), f, b, 204, 158 + y);//寄件账号
        }



        g.DrawString(dt.Rows[0]["Sender"].ToString(), f, b, 10, 222);//寄件人
        g.DrawString(dt.Rows[0]["Sender"].ToString(), f, b, 10, 222 + y);//寄件人
        g.DrawString(dt.Rows[0]["FromPhoneNo"].ToString(), f, b, 204, 222);//电话
        g.DrawString(dt.Rows[0]["FromPhoneNo"].ToString(), f, b, 204, 222 + y);//电话
        g.DrawString(dt.Rows[0]["FlightNo"].ToString(), f, b, 193, 254);//参考号订单号
        g.DrawString(dt.Rows[0]["FlightNo"].ToString(), f, b, 193, 254 + y);//参考号订单号
        g.DrawString(dt.Rows[0]["FromCompany"].ToString(), f, b, 10, 317);//公司名称
        g.DrawString(dt.Rows[0]["FromCompany"].ToString(), f, b, 10, 317 + y);//公司名称

        string address = dt.Rows[0]["FromAddress"].ToString();
        if (address.Length > 30)
        {
            address = address.Insert(28, "\n");
        }

        g.DrawString(address, f, b, 10, 393);//公司地址
        g.DrawString(address, f, b, 10, 393 + y);//公司地址

        if (sep)
        {
            string sender = WayBillService.GetBillSenderBySep(dt.Rows[0]["TrackingNo"].ToString());
            g.DrawString(sender, f, b, 10, 490);//主单收件人
            g.DrawString(sender, f, b, 10, 490 + y);//主单收件人

        }

        g.DrawString(new Tools().StringToTime(dt.Rows[0]["CreateDate"].ToString()).ToShortDateString(), f, b, 228, 524);//制单时间
        g.DrawString(new Tools().StringToTime(dt.Rows[0]["CreateDate"].ToString()).ToShortDateString(), f, b, 228, 524 + y);//制单时间
        //收件人
        g.DrawString(dt.Rows[0]["Receiver"].ToString(), f, b, 377, 184);//收件人
        g.DrawString(dt.Rows[0]["Receiver"].ToString(), f, b, 377, 184 + y);//收件人

        g.DrawString(dt.Rows[0]["ToPhoneNo"].ToString(), f, b, 658, 184);//电话
        g.DrawString(dt.Rows[0]["ToPhoneNo"].ToString(), f, b, 658, 184 + y);//电话

        g.DrawString(dt.Rows[0]["ToCompany"].ToString(), f, b, 535, 222);//公司名称
        g.DrawString(dt.Rows[0]["ToCompany"].ToString(), f, b, 535, 222 + y);//公司名称

        string readdress = dt.Rows[0]["ToAddress"].ToString();
        if (readdress.Length > 25)
        {
            readdress = readdress.Insert(25, "\n");
        }
        g.DrawString(readdress, f, b, 382, 289);//地址
        g.DrawString(readdress, f, b, 382, 289 + y);//地址

        //货物
        DataTable ndt = new PublicSqlService().GetData("*", "TblShipment", "TrackingNo='" + dt.Rows[0]["TrackingNo"].ToString() + "'");
        string name = "文件";
        switch (dt.Rows[0]["Packing"].ToString())
        {
            case "0":
                name = "文件";
                break;
            case "1":
                name = "私人包裹";
                break;
            case "2":
                name = "商业样品";
                break;
        }
        g.DrawString(name, f, b, 459, 414);//货物资料 包装
        g.DrawString(name, f, b, 459, 414 + y);//货物资料 包装

        if (ndt.Rows.Count != 0)
        {
            g.DrawString(ndt.Rows[0]["Commodity"].ToString(), f, b, 391, 489);//名字1
            g.DrawString(ndt.Rows[0]["Commodity"].ToString(), f, b, 391, 489 + y);//名字1

            g.DrawString(ndt.Rows[0]["Qty"].ToString(), f, b, 963, 489);//数量
            g.DrawString(ndt.Rows[0]["Qty"].ToString(), f, b, 963, 489 + y);//数量

            g.DrawString("$" + ndt.Rows[0]["ValueForCustoms"].ToString(), f, b, 1042, 489);//海关申报
            g.DrawString("$" + ndt.Rows[0]["ValueForCustoms"].ToString(), f, b, 1042, 489 + y);//海关申报

            if (ndt.Rows.Count > 1)
            {
                g.DrawString(ndt.Rows[1]["Commodity"].ToString(), f, b, 391, 588);//名字1
                g.DrawString(ndt.Rows[1]["Commodity"].ToString(), f, b, 391, 588 + y);//名字1

                g.DrawString(ndt.Rows[1]["Qty"].ToString(), f, b, 963, 588);//数量2
                g.DrawString(ndt.Rows[1]["Qty"].ToString(), f, b, 963, 588 + y);//数量2

                g.DrawString("$" + ndt.Rows[1]["ValueForCustoms"].ToString(), f, b, 1042, 588);//海关申报2
                g.DrawString("$" + ndt.Rows[1]["ValueForCustoms"].ToString(), f, b, 1042, 588 + y);//海关申报2
            }
        }
        g.DrawString("$" + dt.Rows[0]["DeclarePrice"].ToString(), f, b, 1044, 675);//海关申报ALL
        g.DrawString("$" + dt.Rows[0]["DeclarePrice"].ToString(), f, b, 1044, 675 + y);//海关申报ALL

        g.DrawString(dt.Rows[0]["Origin"].ToString(), f, b, 948, 65);//起运地
        g.DrawString(dt.Rows[0]["Origin"].ToString(), f, b, 948, 65 + y);//起运地

        g.DrawString(dt.Rows[0]["Destination"].ToString(), f, b, 948, 135);//目的地
        g.DrawString(dt.Rows[0]["Destination"].ToString(), f, b, 948, 135 + y);//目的地

        g.DrawString(dt.Rows[0]["NoOfPiece"].ToString(), f, b, 1024, 180);//件数
        g.DrawString(dt.Rows[0]["NoOfPiece"].ToString(), f, b, 1024, 180 + y);//件数


        if (sep)
        {
            g.DrawString(dt.Rows[0]["ChargedWt"].ToString() + "LB", f, b, 1024, 217);//重量LB
            g.DrawString(dt.Rows[0]["ChargedWt"].ToString() + "LB", f, b, 1024, 217 + y);//重量LB
            //1磅＝0.4536 千克
            decimal kh = new Tools().StringToDecimalDif(dt.Rows[0]["ChargedWt"].ToString()) * decimal.Parse("0.4536");

            g.DrawString(kh.ToString("0.00") + "KG", f, b, 1024, 255);//重量KG
            g.DrawString(kh.ToString("0.00") + "KG", f, b, 1024, 255 + y);//重量KG
        }
        else
        {
            g.DrawString(dt.Rows[0]["SepChargedWt"].ToString() + "LB", f, b, 1024, 217);//重量LB
            g.DrawString(dt.Rows[0]["SepChargedWt"].ToString() + "LB", f, b, 1024, 217 + y);//重量LB
            //1磅＝0.4536 千克
            decimal kh = new Tools().StringToDecimalDif(dt.Rows[0]["SepChargedWt"].ToString()) * decimal.Parse("0.4536");

            g.DrawString(kh.ToString("0.00") + "KG", f, b, 1024, 255);//重量KG
            g.DrawString(kh.ToString("0.00") + "KG", f, b, 1024, 255 + y);//重量KG
        }


 

        g.DrawString(dt.Rows[0]["DimensionsL"].ToString() + "×" + dt.Rows[0]["DimensionsW"].ToString() + "×" + dt.Rows[0]["DimensionsH"].ToString(), f, b, 940, 323);//体积重量
        g.DrawString(dt.Rows[0]["DimensionsL"].ToString() + "×" + dt.Rows[0]["DimensionsW"].ToString() + "×" + dt.Rows[0]["DimensionsH"].ToString(), f, b, 940, 323 + y);//体积重量




        //g.DrawString(dt.Rows[0]["ChargedWt"].ToString(), f, b, 975, 249);//收费重量
        //g.DrawString(dt.Rows[0]["ChargedWt"].ToString(), f, b, 975, 249+y);//收费重量


        string payname = PayModelService.GetPayName(dt.Rows[0]["Payment"].ToString());
        g.DrawString(payname, f, b, 941, 402);//支付方式
        g.DrawString(payname, f, b, 941, 402 + y);//支付方式
        g.Dispose();


         */
        #endregion




        int y = 870;


        Graphics g = Graphics.FromImage(image);

        Font f = new Font("宋体", 18, FontStyle.Bold);
        Font bgf = new Font("宋体", 23, FontStyle.Bold);
        Brush b = new SolidBrush(Color.Black);
        g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;


        if (sep)
        {
            g.DrawImage(GetImage(dt.Rows[0]["Logo"].ToString()), 890, 0, 280, 82);
            g.DrawImage(GetImage(dt.Rows[0]["Logo"].ToString()), 890, 0 + y, 280, 82);
        }
        else
        {
            g.DrawImage(GetImage("uxpdflogo.jpg"), 890, 0, 280, 82);
            g.DrawImage(GetImage("uxpdflogo.jpg"), 890, 0 + y, 280, 82);

        }

        g.DrawImage(cd.CreateBarCode("YJ" + dt.Rows[0]["TrackingNo"].ToString().Trim() + "UP", "", 0, 0), 515, 10);//条形码
        g.DrawImage(cd.CreateBarCode("YJ" + dt.Rows[0]["TrackingNo"].ToString().Trim() + "UP", "", 0, 0), 515, 10 + y);//条形码

        g.DrawString("YJ" + dt.Rows[0]["TrackingNo"].ToString() + "UP", bgf, b, 620, 79);//单号
        g.DrawString("YJ" + dt.Rows[0]["TrackingNo"].ToString() + "UP", bgf, b, 620, 79 + y);//单号

        g.DrawString(new Tools().StringToTime(dt.Rows[0]["CreateDate"].ToString()).ToShortDateString(), f, b, 118, 44);//寄件日期
        g.DrawString(new Tools().StringToTime(dt.Rows[0]["CreateDate"].ToString()).ToShortDateString(), f, b, 118, 44 + y);//寄件日期

        if (sep)
        {
            if (dt.Rows[0]["AddressType"].ToString().Trim() != "1")
            {
                //非杭州地区的 需要显示账号
                g.DrawString(dt.Rows[0]["AccountNo"].ToString(), f, b, 380, 45);//寄件账号
                g.DrawString(dt.Rows[0]["AccountNo"].ToString(), f, b, 380, 45 + y);//寄件账号



                g.DrawString(dt.Rows[0]["AccountNo"].ToString(), f, b, 673, 498);//寄件账号
                g.DrawString(dt.Rows[0]["AccountNo"].ToString(), f, b, 673, 498 + y);//寄件账号

                g.DrawString(dt.Rows[0]["AccountNo"].ToString(), f, b, 669, 588);//寄件账号
                g.DrawString(dt.Rows[0]["AccountNo"].ToString(), f, b, 669, 588 + y);//寄件账号
            }

        }
        else
        {
            g.DrawString(dt.Rows[0]["AccountNo"].ToString(), f, b, 380, 45);//寄件账号
            g.DrawString(dt.Rows[0]["AccountNo"].ToString(), f, b, 380, 45 + y);//寄件账号


            g.DrawString(dt.Rows[0]["AccountNo"].ToString(), f, b, 673, 498);//寄件账号
            g.DrawString(dt.Rows[0]["AccountNo"].ToString(), f, b, 673, 498 + y);//寄件账号

            g.DrawString(dt.Rows[0]["AccountNo"].ToString(), f, b, 669, 588);//寄件账号
            g.DrawString(dt.Rows[0]["AccountNo"].ToString(), f, b, 669, 588 + y);//寄件账号

        }
        Tools t = new Tools();


        g.DrawString(dt.Rows[0]["Sender"].ToString(), f, b, 94, 145);//寄件人
        g.DrawString(dt.Rows[0]["Sender"].ToString(), f, b, 94, 145 + y);//寄件人
        g.DrawString(dt.Rows[0]["FromPhoneNo"].ToString(), f, b, 361, 143);//电话
        g.DrawString(dt.Rows[0]["FromPhoneNo"].ToString(), f, b, 361, 143 + y);//电话
        g.DrawString(dt.Rows[0]["FlightNo"].ToString(), f, b, 30, 519);//参考号订单号
        g.DrawString(dt.Rows[0]["FlightNo"].ToString(), f, b, 30, 519 + y);//参考号订单号



        g.DrawString(dt.Rows[0]["FromCompany"].ToString(), f, b, 74, 93);//公司名称
        g.DrawString(dt.Rows[0]["FromCompany"].ToString(), f, b, 74, 93 + y);//公司名称

        string address = dt.Rows[0]["FromAddress"].ToString();
        //if (address.Length > 30)
        //{
        //    address = address.Insert(28, "\n");
        //}

        g.DrawString(address, f, b, 74, 192);//公司地址
        g.DrawString(address, f, b, 74, 192 + y);//公司地址

        //if (sep)
        //{
        //    string sender = WayBillService.GetBillSenderBySep(dt.Rows[0]["TrackingNo"].ToString());
        //    g.DrawString(sender, f, b, 10, 490);//主单收件人
        //    g.DrawString(sender, f, b, 10, 490 + y);//主单收件人

        //}

        //   g.DrawString(new Tools().StringToTime(dt.Rows[0]["CreateDate"].ToString()).ToShortDateString(), f, b, 228, 524);//制单时间
        //  g.DrawString(new Tools().StringToTime(dt.Rows[0]["CreateDate"].ToString()).ToShortDateString(), f, b, 228, 524 + y);//制单时间
        //收件人
        g.DrawString(dt.Rows[0]["Receiver"].ToString(), f, b, 590, 216);//收件人
        g.DrawString(dt.Rows[0]["Receiver"].ToString(), f, b, 590, 216 + y);//收件人

        g.DrawString(dt.Rows[0]["ToPhoneNo"].ToString(), f, b, 885, 216);//电话
        g.DrawString(dt.Rows[0]["ToPhoneNo"].ToString(), f, b, 885, 216 + y);//电话

        g.DrawString(dt.Rows[0]["ToCompany"].ToString(), f, b, 573, 152);//公司名称
        g.DrawString(dt.Rows[0]["ToCompany"].ToString(), f, b, 573, 152 + y);//公司名称

        string readdress = dt.Rows[0]["ToAddress"].ToString();
        //if (readdress.Length > 25)
        //{
        //    readdress = readdress.Insert(25, "\n");
        //}
        g.DrawString(readdress, f, b, 560, 267);//地址
        g.DrawString(readdress, f, b, 560, 267 + y);//地址

        //货物
        DataTable ndt = new PublicSqlService().GetData("*", "TblShipment", "TrackingNo='" + dt.Rows[0]["TrackingNo"].ToString() + "'");
        //string name = "文件";
        //switch (dt.Rows[0]["Packing"].ToString())
        //{
        //    case "0":
        //        name = "文件";
        //        break;
        //    case "1":
        //        name = "私人包裹";
        //        break;
        //    case "2":
        //        name = "商业样品";
        //        break;
        //}
        //g.DrawString(name, f, b, 459, 414);//货物资料 包装
        //g.DrawString(name, f, b, 459, 414 + y);//货物资料 包装
        decimal hg = 0;
        if (ndt.Rows.Count != 0)
        {
            g.DrawString(ndt.Rows[0]["Commodity"].ToString(), f, b, 15, 315);//名字1
            g.DrawString(ndt.Rows[0]["Commodity"].ToString(), f, b, 15, 315 + y);//名字1

            //  g.DrawString(ndt.Rows[0]["Qty"].ToString(), f, b, 963, 489);//数量
            // g.DrawString(ndt.Rows[0]["Qty"].ToString(), f, b, 963, 489 + y);//数量
            hg += t.StringToDecimalDif(ndt.Rows[0]["ValueForCustoms"].ToString());
            //  g.DrawString("$" + ndt.Rows[0]["ValueForCustoms"].ToString(), f, b, 1042, 489);//海关申报
            //  g.DrawString("$" + ndt.Rows[0]["ValueForCustoms"].ToString(), f, b, 1042, 489 + y);//海关申报

            if (ndt.Rows.Count > 1)
            {
                g.DrawString(ndt.Rows[1]["Commodity"].ToString(), f, b, 15, 352);//名字1
                g.DrawString(ndt.Rows[1]["Commodity"].ToString(), f, b, 15, 352 + y);//名字1
                hg += t.StringToDecimalDif(ndt.Rows[1]["ValueForCustoms"].ToString());
                //   g.DrawString(ndt.Rows[1]["Qty"].ToString(), f, b, 963, 588);//数量2
                //  g.DrawString(ndt.Rows[1]["Qty"].ToString(), f, b, 963, 588 + y);//数量2

                //  g.DrawString("$" + ndt.Rows[1]["ValueForCustoms"].ToString(), f, b, 1042, 588);//海关申报2
                //  g.DrawString("$" + ndt.Rows[1]["ValueForCustoms"].ToString(), f, b, 1042, 588 + y);//海关申报2
            }
        }
        g.DrawString("$" + hg.ToString("0.00"), f, b, 27, 559);//海关申报ALL
        g.DrawString("$" + hg.ToString("0.00"), f, b, 27, 559 + y);//海关申报ALL

        //   g.DrawString(dt.Rows[0]["Origin"].ToString(), f, b, 948, 65);//起运地
        //   g.DrawString(dt.Rows[0]["Origin"].ToString(), f, b, 948, 65 + y);//起运地

        //   g.DrawString(dt.Rows[0]["Destination"].ToString(), f, b, 948, 135);//目的地
        //   g.DrawString(dt.Rows[0]["Destination"].ToString(), f, b, 948, 135 + y);//目的地

        //  g.DrawString(dt.Rows[0]["NoOfPiece"].ToString(), f, b, 1024, 180);//件数
        //  g.DrawString(dt.Rows[0]["NoOfPiece"].ToString(), f, b, 1024, 180 + y);//件数


        if (sep)
        {
            g.DrawString(dt.Rows[0]["ChargedWt"].ToString(), f, b, 70, 456);//重量LB
            g.DrawString(dt.Rows[0]["ChargedWt"].ToString(), f, b, 70, 456 + y);//重量LB
            //1磅＝0.4536 千克
            decimal kh = new Tools().StringToDecimalDif(dt.Rows[0]["ChargedWt"].ToString()) * decimal.Parse("0.4536");

            g.DrawString(kh.ToString("0.00"), f, b, 70, 416);//重量KG
            g.DrawString(kh.ToString("0.00"), f, b, 70, 416 + y);//重量KG
        }
        else
        {
            g.DrawString(dt.Rows[0]["SepChargedWt"].ToString(), f, b, 70, 456);//重量LB
            g.DrawString(dt.Rows[0]["SepChargedWt"].ToString(), f, b, 70, 456 + y);//重量LB
            //1磅＝0.4536 千克
            decimal kh = new Tools().StringToDecimalDif(dt.Rows[0]["SepChargedWt"].ToString()) * decimal.Parse("0.4536");

            g.DrawString(kh.ToString("0.00"), f, b, 70, 416);//重量KG
            g.DrawString(kh.ToString("0.00"), f, b, 70, 416 + y);//重量KG
        }




        g.DrawString(dt.Rows[0]["DimensionsL"].ToString() + "×" + dt.Rows[0]["DimensionsW"].ToString() + "×" + dt.Rows[0]["DimensionsH"].ToString(), f, b, 355, 346);//体积重量
        g.DrawString(dt.Rows[0]["DimensionsL"].ToString() + "×" + dt.Rows[0]["DimensionsW"].ToString() + "×" + dt.Rows[0]["DimensionsH"].ToString(), f, b, 355, 346 + y);//体积重量

        if (sep)
        {

            decimal septj = t.StringToDecimalDif(dt.Rows[0]["DimensionsL"].ToString()) * t.StringToDecimalDif(dt.Rows[0]["DimensionsW"].ToString()) * t.StringToDecimalDif(dt.Rows[0]["DimensionsH"].ToString());

            decimal septjlb = septj / decimal.Parse("165");
            decimal sepkg = septjlb / decimal.Parse("2.2");

            g.DrawString(septjlb.ToString("0.00"), f, b, 187, 456);// 体积重量 lb

            g.DrawString(septjlb.ToString("0.00"), f, b, 187, 456 + y);// 体积重量 lb



            g.DrawString(sepkg.ToString("0.00"), f, b, 187, 416);//体积重量KG
            g.DrawString(sepkg.ToString("0.00"), f, b, 187, 416 + y);//体积重量KG

        }
        else
        {
            g.DrawString(dt.Rows[0]["DimensionsWeight"].ToString(), f, b, 187, 456);// 体积重量 lb

            g.DrawString(dt.Rows[0]["DimensionsWeight"].ToString(), f, b, 187, 456 + y);// 体积重量 lb

            decimal dwlb = new Tools().StringToDecimalDif(dt.Rows[0]["DimensionsWeight"].ToString()) * decimal.Parse("0.4536");

            g.DrawString(dwlb.ToString("0.00"), f, b, 187, 416);//体积重量KG
            g.DrawString(dwlb.ToString("0.00"), f, b, 187, 416 + y);//体积重量KG
        }

        //g.DrawString(dt.Rows[0]["ChargedWt"].ToString(), f, b, 975, 249);//收费重量
        //g.DrawString(dt.Rows[0]["ChargedWt"].ToString(), f, b, 975, 249+y);//收费重量


        // string payname = PayModelService.GetPayName(dt.Rows[0]["Payment"].ToString());


        switch (dt.Rows[0]["Payment"].ToString().Trim())
        {
            case "1":
                g.DrawString("■", f, b, 540, 479);//支付方式 发送者
                g.DrawString("■", f, b, 540, 479 + y);//支付方式 发送者


                g.DrawString("■", f, b, 540, 565);//支付方式 发送者
                g.DrawString("■", f, b, 540, 565 + y);//支付方式 发送者

                break;
            case "2":

                g.DrawString("■", f, b, 540, 479);//支付方式 发送者
                g.DrawString("■", f, b, 540, 479 + y);//支付方式 发送者


                g.DrawString("■", f, b, 540, 565);//支付方式 发送者
                g.DrawString("■", f, b, 540, 565 + y);//支付方式 发送者
                break;
            case "3":

                g.DrawString("■", f, b, 626, 479);//支付方式 收获者
                g.DrawString("■", f, b, 626, 479 + y);//支付方式 收获者


                g.DrawString("■", f, b, 626, 565);//支付方式 收获者
                g.DrawString("■", f, b, 626, 565 + y);//支付方式 收获者

                break;
            case "4":
                g.DrawString("■", f, b, 700, 479);//支付方式 第三方
                g.DrawString("■", f, b, 700, 479 + y);//支付方式 第三方

                g.DrawString("■", f, b, 700, 565);//支付方式 第三方
                g.DrawString("■", f, b, 700, 565 + y);//支付方式 第三方
                break;

            default:
                break;
        }








        g.Dispose();





    }
    void ToResponse(string path)
    {


        FileInfo fi = new FileInfo(path);//excelFile为文件在服务器上的地址
        HttpResponse contextResponse = HttpContext.Current.Response;
        contextResponse.Clear();
        contextResponse.Buffer = true;
        contextResponse.Charset = "utf-8"; //设置了类型为中文防止乱码的出现 
        string filename = HttpUtility.UrlEncode("单据打印.pdf");
        contextResponse.AppendHeader("Content-Disposition", String.Format("attachment;filename={0}", filename)); //定义输出文件和文件名 
        contextResponse.AppendHeader("Content-Length", fi.Length.ToString());
        contextResponse.ContentEncoding = Encoding.Default;
        contextResponse.ContentType = "application/pdf";//设置输出文件类型为excel文件。 
        contextResponse.WriteFile(fi.FullName);
        contextResponse.Flush();
        contextResponse.End();
    }

    private void ViewPdf(Stream fs)
    {
        Response.Clear();
        //中文名的话
        //Response.AppendHeader("Content-Disposition", "attachment;filename=" +
        //             HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ";charset=GB2312");
        Response.AddHeader("Content-Disposition", "attachment;FileName=Bill.pdf");
        Response.AddHeader("Content-Length", fs.Length.ToString());
        Response.ContentType = "application/pdf";
        long fileLength = fs.Length;
        int size = 10240;//10K一--分块下载，10K为1块
        byte[] readData = new byte[size];
        if (size > fileLength)
            size = Convert.ToInt32(fileLength);
        long fPos = 0;
        bool isEnd = false;
        while (!isEnd)
        {
            if ((fPos + size) >= fileLength)
            {
                size = Convert.ToInt32(fileLength - fPos);
                isEnd = true;
            }
            readData = new byte[size];
            fs.Position = fPos;
            fs.Read(readData, 0, size);
            Response.BinaryWrite(readData);
            Response.OutputStream.Flush();
            fPos += size;
        }
        fs.Close();
        Response.OutputStream.Close();
        Response.End();//非常重要，没有这句的话，页面的HTML代码将会保存到文件中
        Response.Close();
    }
    System.Drawing.Image GetImage(string url)
    {

        if (url.Trim() == "")
        {
            return null;
        }
        try
        {
            System.Drawing.Image oImage = System.Drawing.Image.FromFile(ApplicationInfo.PhysicalApplicationPath + "UpLoad/" + url);

            return oImage;
        }
        catch
        {
            System.Drawing.Image oImage = System.Drawing.Image.FromFile(ApplicationInfo.PhysicalApplicationPath + "UpLoad/uxpdflogo.jpg");

            return oImage;
        }


    }
    System.Drawing.Image GetBkImage()
    {


        Bitmap bmp = new Bitmap(800, 50); //创建一张空白图片
        //        Bitmap bmp = new Bitmap(800, 100); //创建一张空白图片
        Graphics g = Graphics.FromImage(bmp);

        g.DrawImage(bmp, 0, 0, 800, 100);
        g.Clear(Color.White);


        return bmp;


    }

}
